//
//  NewsLunboViewController.m
//  WangYi
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 0. All rights reserved.
//

#import "NewsLunboViewController.h"
#import "LunBoModel.h"
#import "NewsLunBoCollectionViewCell.h"
@interface NewsLunboViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)NSMutableArray *allArray;

@property(nonatomic,strong)UICollectionView *myCollectionView;

@end

@implementation NewsLunboViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.allArray = [NSMutableArray array];
   self.navigationItem.leftBarButtonItem = [[ UIBarButtonItem alloc ]initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(leftAction)];
   
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc]init];
    flowLayOut.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayOut.itemSize = CGSizeMake(WIDTH*365, HEIGHT* 500);
    flowLayOut.minimumInteritemSpacing = WIDTH * 10 ;
    flowLayOut.minimumLineSpacing = WIDTH * 10;
    flowLayOut.sectionInset = UIEdgeInsetsMake(WIDTH*5, HEIGHT*10, WIDTH*5, HEIGHT*10);
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:flowLayOut];
    [self.view addSubview:self.myCollectionView];
    
    [self.myCollectionView registerClass:[NewsLunBoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource =self;
    
    // Do any additional setup after loading the view.
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 1;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return self.allArray.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    NewsLunBoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blackColor];
    LunBoModel *model = [self.allArray objectAtIndex:indexPath.row];
    
    cell.model = model;
    return cell;
}


-(void)leftAction{

    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    

}
-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    
       
    [self getDetail];
    
}


-(void)getDetail{

    [HTTPTool get:self.URL body:nil httpResult:JSON success:^(id result) {
        
        NSMutableDictionary *Dic = result;
        
        NSMutableArray *Array = [Dic objectForKey:@"photos"];
        for (NSMutableDictionary *dic  in Array) {
            
            
            LunBoModel *model = [[LunBoModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.allArray addObject:model];
            
        }
        
        
        [self.myCollectionView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"error=%@",error);
        
    }];

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
